namespace CIS.Resource
{
    using CISLib;
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class ResourceRestaurantPageEdit : Page
    {
        protected Image iResource;
        protected Label lbRestaurantAddress;
        protected Label lbRestaurantCity;
        protected Label lbRestaurantDiscount;
        protected Label lbRestaurantFax;
        protected Label lbRestaurantFeature;
        protected Label lbRestaurantIsCreditCard;
        protected Label lbRestaurantName;
        protected Label lbRestaurantPhone;
        protected Label lbRestaurantProvince;
        protected Label lbRestaurantRecommendationRate;
        protected UserPrincipal Principal = null;
        protected TextBox RestaurantAddressTxb;
        protected DropDownList RestaurantCityDdl;
        protected TextBox RestaurantDiscountTxb;
        protected TextBox RestaurantFaxTxb;
        protected TextBox RestaurantFeatureTxb;
        protected DropDownList RestaurantIsCreditCardDdl;
        protected TextBox RestaurantNameTxb;
        protected TextBox RestaurantPhoneTxb;
        protected DropDownList RestaurantProvinceDdl;
        protected DropDownList RestaurantRecommendationRateDdl;
        protected Button SaveBtn;

        private void FillPage()
        {
            if (base.Request.QueryString["IsModify"] == "True")
            {
                Guid id = new Guid(base.Request.QueryString["RestaurantId"]);
                ResourceRestaurant resourceRestaurant = ManageResource.GetResourceRestaurantInfoViaId(id);
                this.RestaurantNameTxb.Text = resourceRestaurant.RestaurantName;
                this.RestaurantAddressTxb.Text = resourceRestaurant.RestaurantAddress;
                this.RestaurantPhoneTxb.Text = resourceRestaurant.RestaurantPhone;
                this.RestaurantFaxTxb.Text = resourceRestaurant.RestaurantFax;
                this.RestaurantDiscountTxb.Text = resourceRestaurant.RestaurantDiscount;
                this.RestaurantFeatureTxb.Text = resourceRestaurant.RestaurantFeature;
                for (int i = 0; i < this.RestaurantProvinceDdl.Items.Count; i++)
                {
                    if (this.RestaurantProvinceDdl.Items[i].Value == resourceRestaurant.RestaurantProvince.ToString())
                    {
                        this.RestaurantProvinceDdl.SelectedIndex = i;
                    }
                }
                this.RestaurantCityDdl.DataSource = ManageCity.GetCityViaProvinceId(resourceRestaurant.RestaurantProvince);
                this.RestaurantCityDdl.DataTextField = "regionName";
                this.RestaurantCityDdl.DataValueField = "regionId";
                this.RestaurantCityDdl.DataBind();
                bool result = false;
                for (int i = 0; i < this.RestaurantCityDdl.Items.Count; i++)
                {
                    if (this.RestaurantCityDdl.Items[i].Value == resourceRestaurant.RestaurantCity.ToString())
                    {
                        this.RestaurantCityDdl.SelectedIndex = i;
                        result = true;
                        break;
                    }
                }
                if (!result)
                {
                    this.RestaurantCityDdl.Items.Clear();
                }
                for (int i = 0; i < this.RestaurantCityDdl.Items.Count; i++)
                {
                    if (this.RestaurantCityDdl.Items[i].Value == resourceRestaurant.RestaurantCity.ToString())
                    {
                        this.RestaurantCityDdl.SelectedIndex = i;
                    }
                }
                for (int i = 0; i < this.RestaurantIsCreditCardDdl.Items.Count; i++)
                {
                    if (this.RestaurantIsCreditCardDdl.Items[i].Text == resourceRestaurant.RestaurantIsCreditCard)
                    {
                        this.RestaurantIsCreditCardDdl.SelectedIndex = i;
                    }
                }
                for (int i = 0; i < this.RestaurantRecommendationRateDdl.Items.Count; i++)
                {
                    if (this.RestaurantRecommendationRateDdl.Items[i].Value == resourceRestaurant.RestaurantRecommendationRate)
                    {
                        this.RestaurantRecommendationRateDdl.SelectedIndex = i;
                    }
                }
            }
        }

        private void InitializeComponent()
        {
            this.RestaurantProvinceDdl.SelectedIndexChanged += new EventHandler(this.RestaurantProvinceDdl_SelectedIndexChanged);
            this.SaveBtn.Click += new EventHandler(this.SaveBtn_Click);
            base.Load += new EventHandler(this.Page_Load);
        }

        private void InitPage()
        {
            this.SetFromImage();
            this.SetFrom();
            this.RestaurantIsCreditCardDdl.Items.Clear();
            ListItem temItem1 = new ListItem();
            temItem1.Text = "是";
            ListItem temItem2 = new ListItem();
            temItem2.Text = "否";
            ListItem temItem3 = new ListItem();
            temItem3.Text = "不明";
            this.RestaurantIsCreditCardDdl.Items.Add(temItem1);
            this.RestaurantIsCreditCardDdl.Items.Add(temItem2);
            this.RestaurantIsCreditCardDdl.Items.Add(temItem3);
            this.RestaurantProvinceDdl.DataSource = ManageProvince.GetProvinceNoAll();
            this.RestaurantProvinceDdl.DataTextField = "RegionName";
            this.RestaurantProvinceDdl.DataValueField = "RegionId";
            this.RestaurantProvinceDdl.DataBind();
            this.RestaurantCityDdl.DataSource = ManageCity.GetCityViaProvinceId(new Guid(this.RestaurantProvinceDdl.SelectedItem.Value));
            this.RestaurantCityDdl.DataTextField = "regionName";
            this.RestaurantCityDdl.DataValueField = "regionId";
            this.RestaurantCityDdl.DataBind();
            string stri = "";
            for (int i = 1; i <= 5; i++)
            {
                ListItem item = new ListItem();
                item.Value = i.ToString();
                stri = stri + "*";
                item.Text = stri;
                this.RestaurantRecommendationRateDdl.Items.Add(item);
            }
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
            this.Principal = this.Context.User as UserPrincipal;
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                this.SelectAuth();
            }
        }

        private void RestaurantProvinceDdl_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.RestaurantCityDdl.DataSource = ManageCity.GetCityViaProvinceId(new Guid(this.RestaurantProvinceDdl.SelectedItem.Value));
            this.RestaurantCityDdl.DataTextField = "regionName";
            this.RestaurantCityDdl.DataValueField = "regionId";
            this.RestaurantCityDdl.DataBind();
        }

        private void SaveBtn_Click(object sender, EventArgs e)
        {
            string strRestaurantName = this.RestaurantNameTxb.Text.Trim();
            string strRestaurantAddress = this.RestaurantAddressTxb.Text.Trim();
            string strRestaurantPhone = this.RestaurantPhoneTxb.Text.Trim();
            string strRestaurantFax = this.RestaurantFaxTxb.Text.Trim();
            string strRestaurantDiscount = this.RestaurantDiscountTxb.Text.Trim();
            string strRestaurantFeature = this.RestaurantFeatureTxb.Text.Trim();
            if (base.Request.QueryString["IsModify"] == "True")
            {
                if (strRestaurantName != "")
                {
                    Guid cityId = new Guid("00000000-0000-0000-0000-000000000001");
                    if (this.RestaurantCityDdl.Items.Count != 0)
                    {
                        cityId = new Guid(this.RestaurantCityDdl.SelectedItem.Value);
                    }
                    ManageResource.UpdateResourceRestaurantInfo(new Guid(base.Request.QueryString["RestaurantId"]), strRestaurantName, new Guid(this.RestaurantProvinceDdl.SelectedItem.Value), cityId, strRestaurantAddress, strRestaurantPhone, strRestaurantFax, this.RestaurantIsCreditCardDdl.SelectedItem.Text, strRestaurantDiscount, strRestaurantFeature, this.RestaurantRecommendationRateDdl.SelectedItem.Value);
                    base.Response.Redirect("~/Resource/ResourceRestaurantPage.aspx", true);
                }
                base.Response.Write("<script> alert('Name is Null !') </script>");
            }
            else if (strRestaurantName != "")
            {
                if (!ManageResource.IsExistSameResourceRestaurantInfo(strRestaurantName))
                {
                    Guid cityId = new Guid("00000000-0000-0000-0000-000000000001");
                    if (this.RestaurantCityDdl.Items.Count != 0)
                    {
                        cityId = new Guid(this.RestaurantCityDdl.SelectedItem.Value);
                    }
                    ManageResource.InsertNewResourceRestaurantInfo(strRestaurantName, new Guid(this.RestaurantProvinceDdl.SelectedItem.Value), cityId, strRestaurantAddress, strRestaurantPhone, strRestaurantFax, this.RestaurantIsCreditCardDdl.SelectedItem.Text, strRestaurantDiscount, strRestaurantFeature, this.RestaurantRecommendationRateDdl.SelectedItem.Value);
                    base.Response.Redirect("~/Resource/ResourceRestaurantPage.aspx", true);
                }
                else
                {
                    base.Response.Write("<script> alert('该信息已存在') </script>");
                }
            }
        }

        private void SelectAuth()
        {
            if (this.Principal.OwnRole.TypeC == 0)
            {
                this.InitPage();
                this.FillPage();
                this.SaveBtn.Visible = false;
            }
            else if (this.Principal.OwnRole.TypeC == 1)
            {
                this.InitPage();
                this.FillPage();
            }
        }

        private void SetFrom()
        {
            string language = this.Session["language"].ToString();
            string path = base.Server.MapPath("~/LanguageConfig.xml");
            this.lbRestaurantName.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceRestaurantPageEdit", "lbRestaurantName", language, path);
            if (this.Principal.OwnArea[0].AreaId.ToString() == "a7f26634-10a9-4cb1-aa2c-8c91a8dfdd66")
            {
                this.lbRestaurantProvince.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceRestaurantPageEdit", "lbRestaurantProvinceOverSea", language, path);
                this.lbRestaurantCity.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceRestaurantPageEdit", "lbRestaurantCityOverSea", language, path);
            }
            else
            {
                this.lbRestaurantProvince.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceRestaurantPageEdit", "lbRestaurantProvince", language, path);
                this.lbRestaurantCity.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceRestaurantPageEdit", "lbRestaurantCity", language, path);
            }
            this.lbRestaurantAddress.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceRestaurantPageEdit", "lbRestaurantAddress", language, path);
            this.lbRestaurantPhone.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceRestaurantPageEdit", "lbRestaurantPhone", language, path);
            this.lbRestaurantFax.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceRestaurantPageEdit", "lbRestaurantFax", language, path);
            this.lbRestaurantIsCreditCard.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceRestaurantPageEdit", "lbRestaurantIsCreditCard", language, path);
            this.lbRestaurantDiscount.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceRestaurantPageEdit", "lbRestaurantDiscount", language, path);
            this.lbRestaurantFeature.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceRestaurantPageEdit", "lbRestaurantFeature", language, path);
            this.lbRestaurantRecommendationRate.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceRestaurantPageEdit", "lbRestaurantRecommendationRate", language, path);
            this.SaveBtn.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceRestaurantPageEdit", "SaveBtn", language, path);
        }

        private void SetFromImage()
        {
            switch (this.Session["language"].ToString())
            {
                case "cn":
                    this.iResource.ImageUrl = "../images/title_SharedInfo.jpg";
                    break;

                case "en":
                    this.iResource.ImageUrl = "../images/title_SharedInfoEn.jpg";
                    break;
            }
        }
    }
}

